#include "MBDirectoryMethods.h"
#include "MBStringMethods.h"
#include "dirent.h"

#include <algorithm>
#include <sys/stat.h>

using std::string;
using std::transform;
using std::vector;

/**
  * Returns a vector with all Jpeg- and PNG-filenames inside the directory that is passed as an argument.
  *
  * @param strDir full path of the directory that is used for the image search. No directories inside this directory are considered.
  * @return vector with all Jpeg- and PNG-filenames inside the directory
  */
vector<string> MB::GetAllImageNamesInsideDir( const string &strDir )
{
	vector<string> imageNames;

	if (!IsDir( strDir ))
		return imageNames;

	string str;
	DIR *p_dir;
	struct dirent *entry;

	if (p_dir = opendir( strDir.c_str() ))
	{
		while (entry = readdir( p_dir ))
		{
			if (strcmp( entry->d_name, "." ) != 0 && 
				strcmp( entry->d_name, ".." ) != 0 &&
				(GetFileExtensionLowerCase( entry->d_name ) == "jpg" ||
				GetFileExtensionLowerCase( entry->d_name ) == "jpeg" ||
				GetFileExtensionLowerCase( entry->d_name ) == "png" ||
				GetFileExtensionLowerCase( entry->d_name ) == "tif" ||
				GetFileExtensionLowerCase( entry->d_name ) == "tiff" ||
				GetFileExtensionLowerCase( entry->d_name ) == "bmp") )
			{
				imageNames.push_back( entry->d_name );
			}
		}

		closedir( p_dir );
	}

	return imageNames;
}

/**
  *	Returns the base path of the file, whose full path is passed as an argument.
  * For c:\baseDir\file1.txt this would be c:\baseDir
  *
  * @param filePath const reference to the full path of the file
  * @return base path of the file
  */
string MB::GetFileBasePath( const string &filePath )
{
	string::size_type index = filePath.find_last_of( "/\\" );

	if (index == string::npos)
		return filePath;

	return filePath.substr( 0, index );
}

/**
  *	Returns the lower case representation of the file extension for a 
  * full path. For c:\file.TXT this would be "txt"
  *
  * @param filePath path of a file
  * @return lower case representation of the file extension
  */
string MB::GetFileExtensionLowerCase( const string &filePath )
{
	string::size_type index = filePath.rfind( "." );

	if (index == string::npos)
		return "";

	string result = filePath.substr( index + 1 );
	transform( result.begin(), result.end(), result.begin(), ::tolower);

	return result;
}

/**
  *	Returns the file name from a path. For c:\dir\file.txt this would be
  * fil.txt
  *
  * @param filePath path of a file
  * @return file name from the path
  */
string MB::GetFileNameFromPath( const string &filePath )
{
	string::size_type index = filePath.find_last_of( "/\\" );

	if (index == filePath.npos)
		return filePath;
	else
		return filePath.substr( index + 1 );
}

/**
  * Returns a boolean value indicating whether the argument corresponds to an
  * existing directory or not.
  *
  * @param strDir string to check whether it corresponds to an existing directory or not
  * @return boolean value indicating whether the argument corresponds to an existing directory or not
  */
bool MB::IsDir( const string &strDir )
{	
	struct stat buffer;   
	return (stat( strDir.c_str(), &buffer ) == 0);
}

/**
  *	Returns true when the file corresponding to the given file path exists,
  * false otherwise
  *
  * @param filePath path of a file
  * @return true when the file corresponding to the given file path exists, false otherwise
  */
bool MB::IsFile( const std::string &filePath )
{
	struct stat buffer;   
	return (stat( filePath.c_str(), &buffer ) == 0);
}

/**
  *	Removes the file extension from the path that is passed as an argument.
  * For c:\baseDir\file1.txt this would be c:\baseDir\file1
  *
  * @param filePath const reference to the full path of the file
  * @return path of the file without file extension
  */
string MB::RemoveFileExtension( const string &filePath )
{
	string::size_type index = filePath.rfind( "." );

	if (index == string::npos)
		return filePath;

	return filePath.substr( 0, index );
}